﻿using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;

namespace MySQLProvider.Utility
{
    public class MYSQLUtility 

    {
        public static DataSet GetTableNames(string connectStr)
        {
            List<string> tables = new List<string>();

            DataSet dtSet = new DataSet();
            using (MySqlConnection con = new MySqlConnection(connectStr))
            {
                MySqlCommand command = new MySqlCommand("show tables;", con);
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                adapter.Fill(dtSet);
            }
            return dtSet;

            
        }

        public static DataSet GetTableFields(string connectStr,string tableName)
        {

            DataSet dtSet = new DataSet();
            using (MySqlConnection con = new MySqlConnection(connectStr))
            {
                //string.Format("describe {0};", tableName)
                string sql = string.Format( @"SELECT  COLUMN_NAME as Field, COLUMN_TYPE as Type, IS_NULLABLE as `Null`, COLUMN_KEY as `Key`,COLUMN_DEFAULT as `Default`, Extra, COLUMN_COMMENT as `Comment` FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{0}' AND table_schema = '{1}'",
                    tableName.ToLower(), con.Database.ToLower());

                MySqlCommand command = new MySqlCommand(sql, con);
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                adapter.Fill(dtSet);
            }
            return dtSet;

           

        }     

    }
}
